#pragma once
#include <qbtools/message/MessagePoster.h>
#include <widgets/SPopupWindow.h>

#include <QLabel>

#include "../auth.h"
#include "MobileCfmVerifyDlg.h"
class QGridLayout;
class QLabel;
class SLineEdit;
class SImage;
class QSpacerItem;
class SCheckComboBox;
class MobileCfmDlg;
class MobileCfmQrLabel;
class MobileCfmVerifyDlg;
class MobileCfmUserInfoDlg;
enum em_MobileCfmStep {
  step_Start = 0,
  step_Verify_Mobile,  // 手机验证
  step_Verify_Info,    // 验证个人信息
  step_Success,        // 重置成功
  step_All
};

class SMobileVerifyLabel : public QLabel {
  Q_OBJECT
 public:
  SMobileVerifyLabel(const QString& t, QWidget* parent /*= nullptr*/);
  ~SMobileVerifyLabel() = default;
 signals:
  void clicked();

 protected:
  bool event(QEvent* e) override;
};

class S_AUTH_EXPORT MobileConfirmDlg : public SPopupWindow,
                                       public MessageReceiver {
  Q_OBJECT
 public:
  MobileConfirmDlg(QWidget* p = nullptr);
  ~MobileConfirmDlg() = default;

 protected:
  virtual void paintEvent(QPaintEvent* event) override;
  void setStep(em_MobileCfmStep step);
  void hideMobileCfmWidget(em_MobileCfmStep step);
  bool verifyGetMessage(bool isQuickVerify = false);  // 获取手机验证码验证信息
  bool verifyPwd() const;
  bool verifyMobile() const;
  bool verifyUserInfo();
  void verifyCodeMsg();
  void beginMessageTimer();  // 验证码定时器
  void beginMobileVerifyTimer();
  void beginSubmitTimer();
  void reEnableTimerButton();
  void enableButton(QPushButton* btn, bool enable, QString txt);
  void requestMobileSendMsg();
  void requestMobileCodeMsg();
  void requestNextTimeMsg();
  void requestSubmitInfoMsg();
  void loadRemainTimes();
  QString getRemainTimeText();
  void onDataArrived(const qb::SSAckMsg& msg) override;

 signals:
  void mobileSendCodeData(int);  // 验证码
  void mobileVerifyCode(bool);
  void mobileModifyUserInfo();
 protected slots:
  void onGetCode();
  void onQuickVerify();
  void onNextTime();
  void onCountDown();
  void onSumbit();
  void onDealCodeData(int data);
  void onRefreshUI(bool isPassVerify);
  void onSuccessSubmit();

 private:
  QVBoxLayout* m_mainLayout = nullptr;
  MobileCfmDlg* m_mobileCfmDlg = nullptr;
  QSpacerItem* m_sVItem = nullptr;
  QSpacerItem* m_fVItem = nullptr;
  em_MobileCfmStep m_step;
  QString m_userInfoMsg = "";  // 验证个人信息错误信息提示
  QString m_mobileMsg = "";    // step_Verify_Mobile中的信息提示
  QString m_remainMsg = "";
  MobileCfmVerifyDlg* m_mobileVerify = nullptr;
  MobileCfmUserInfoDlg* m_mobileUserInfo = nullptr;
  int m_nMessageTime;  // 验证码计时
  int m_nRemainTimes = 5;
  QTimer* m_timeCountdown = nullptr;  // 验证码倒计时定时器
  QTimer* m_quickVerifyTimer =
      nullptr;  // step_Verify_Mobile中点击立即验证,触发定时器
  QTimer* m_submitTimer = nullptr;
  QTimer* m_dlgCloseTimer = nullptr;
};

class MobileCfmDlg : public QWidget {
  Q_OBJECT
 public:
  MobileCfmDlg(QWidget* p = nullptr);
  QPushButton* m_verify = nullptr;
  SMobileVerifyLabel* m_nextTime = nullptr;
};

// 二维码及label
class MobileCfmQrLabel : public QWidget {
  Q_OBJECT
 public:
  MobileCfmQrLabel(QWidget* p = nullptr);
};